package com.liuxl.wqdc.controller;

import com.liuxl.wqdc.DTO.ResponseDTO;
import com.liuxl.wqdc.entity.Exam;
import com.liuxl.wqdc.service.ExamService;
import com.liuxl.wqdc.service.ManagerService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@CrossOrigin(origins = "*")
public class ExamController {
    @Autowired
    ExamService examService;
    @Autowired
    ManagerService managerService;

    @RequestMapping("/findExamList")
    @ResponseBody
    public JSONObject findExamList() {
        List<Exam> examList = examService.findAll();
        JSONObject jsonObject = JSONObject.fromObject(new ResponseDTO(200, "success"));
        jsonObject.put("data", JSONArray.fromObject(examList));
        return jsonObject;
    }

    @RequestMapping("/findExamById")
    @ResponseBody
    public JSONObject findExamLById(@RequestParam Integer examId) {
        JSONObject data = examService.getExamDetails(examId);
        JSONObject jsonObject = JSONObject.fromObject(new ResponseDTO(200, "success"));
        jsonObject.put("data", data);
        return jsonObject;
    }

    @RequestMapping(value = "/postExamData", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject postExamData(@RequestParam Integer examId, @RequestParam String studentName, @RequestParam String resList,@RequestParam String finishYears) {
        examService.saveRecord(examId, studentName, resList,finishYears);
        JSONObject jsonObject = JSONObject.fromObject(new ResponseDTO(200, "success"));
        return jsonObject;
    }

    @RequestMapping("/analyze")
    @ResponseBody
    public JSONObject analyzeData(@RequestParam Integer examId) {
        JSONArray jsonArray = managerService.dataAnalyze(examId);
        JSONObject jsonObject = JSONObject.fromObject(new ResponseDTO(200, "success"));
        jsonObject.put("data",jsonArray);
        return  jsonObject;
    }

    @RequestMapping("/findAllRecordByExamId")
    @ResponseBody
    public JSONObject findAllRecordByExamId(@RequestParam Integer examId,@RequestParam Integer page,@RequestParam Integer pageSize){
        JSONObject data=managerService.findAllRecordByExamId(examId,page,pageSize);
        JSONObject jsonObject = JSONObject.fromObject(new ResponseDTO(200, "success"));
        jsonObject.put("data",data);
        return  jsonObject;
    }
    @RequestMapping(value="/deleteRecord",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject deleteRecord(@RequestParam String wenquanId){
        managerService.deleteRecord(wenquanId);
        JSONObject jsonObject = JSONObject.fromObject(new ResponseDTO(200, "success"));
        return  jsonObject;
    }

    @RequestMapping("/findRecordByFinishYears")
    @ResponseBody
    public JSONObject findRecordByFinishYears(@RequestParam Integer examId,@RequestParam String finishYears,@RequestParam Integer page,@RequestParam Integer pageSize){
        JSONObject recordByStudentName = managerService.findRecordByFinishYears(examId, finishYears, page, pageSize);
        JSONObject jsonObject = JSONObject.fromObject(new ResponseDTO(200, "success"));
        jsonObject.put("data",recordByStudentName);
        return  jsonObject;
    }

    @RequestMapping("/findQuestionInfoByWenquanId")
    @ResponseBody
    public JSONObject findQuestionInfoByWenquanId(@RequestParam String wenquanId,@RequestParam Integer examId){
        JSONObject recordByStudentName = managerService.findQuestionInfoByWenquanId(wenquanId,examId);
        JSONObject jsonObject = JSONObject.fromObject(new ResponseDTO(200, "success"));
        jsonObject.put("data",recordByStudentName);
        return  jsonObject;
    }
}

